package com.soonfor.evaluate.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.estronger.shopmanage.R;
import com.soonfor.evaluate.tools.ImageUtils;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.ArrayList;

import uk.co.senab.photoview.PhotoView;

public class ShowPicActivity extends AppCompatActivity {

    private ViewPager mPager;
    private PagerAdapter pagerAdapter;
    private ArrayList<String> pics;
    private ArrayList<String> deletePics;//删除的列表
    private ImageView iv_return;
    private TextView clickDelete;
    //    private TextView txt_pageno;
    private int position;
    private boolean isLocalPic;//是不是本地图片
    private boolean isCanDelete = false;//是否可删除
    private boolean isDoDeleteBtn = false;//是否有删除操作
    public final static int CODE_DELETE_FINISH = 20219;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_showpic);
        setImmersive();
        iv_return = findViewById(R.id.iv_return);
        clickDelete = findViewById(R.id.clickDelete);
//        txt_pageno = (TextView) findViewById(R.id.txt_pageno);
        try {
            isCanDelete = getIntent().getBooleanExtra("ifCanDelete", false);
        }catch (Exception e){
            isCanDelete = false;
        }
        if (isCanDelete) clickDelete.setVisibility(View.VISIBLE);
        else clickDelete.setVisibility(View.GONE);
        isDoDeleteBtn = false; deletePics = new ArrayList<>();
        pics = getIntent().getStringArrayListExtra("piclists");
        position = getIntent().getIntExtra("position",0);
        try {
            isLocalPic = getIntent().getBooleanExtra("ISLOCALPIC", false);
        }catch (Exception e){
            isLocalPic = false;
        }
//        if (pics != null && pics.size() > 0) {
//            txt_pageno.setText((position+1) + "/" + pics.size());
//        } else {
//            txt_pageno.setText("");
//        }

        mPager = (ViewPager) findViewById(R.id.pager);
        mPager.setPageMargin((int) (getResources().getDisplayMetrics().density * 15));
        pagerAdapter = new PagerAdapter() {
            @Override
            public int getCount() {
                return pics.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
//                if (pics.size() == 0) {
//                    txt_pageno.setText("");
//                }else {
//                    txt_pageno.setText(position + 1 + "/" + pics.size());
//                }
                PhotoView view = new PhotoView(ShowPicActivity.this);
                view.setEnabled(true);
                view.setScaleType(ImageView.ScaleType.FIT_CENTER);
                String path = pics.get(position);
                if(isLocalPic && !(path.startsWith("http://") || path.startsWith("https://"))){
                    Picasso.with(ShowPicActivity.this).load(new File(pics.get(position)))
                            .placeholder(R.drawable.no_picture_icon)
                            .error(R.drawable.no_picture_icon)
                            .into(view);
                }else {
                    ImageUtils.loadImage(ShowPicActivity.this, pics.get(position), view);
                }
                container.addView(view);
                container.setBackgroundColor(getResources().getColor(R.color.unselect_color));
                return view;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
            }
        };
        mPager.setAdapter(pagerAdapter);
        mPager.setCurrentItem(position);
        iv_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isCanDelete && isDoDeleteBtn) {
                    Intent intent = new Intent();
                    intent.putExtra("data_list", pics);
                    setResult(Activity.RESULT_OK, intent);
                    finish();
                }else {
                    finish();
                }
            }
        });
        clickDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(pics != null && mPager.getCurrentItem() >= 0 && mPager.getCurrentItem() < pics.size()) {
                    isDoDeleteBtn = true;
                    deletePics.add(pics.get(mPager.getCurrentItem()));
                    pics.remove(mPager.getCurrentItem());
                    pagerAdapter.notifyDataSetChanged();
                    if(pics.size() == 0) {
//                        txt_pageno.setText("");
                        iv_return.callOnClick();
                    }else {
                        mPager.setCurrentItem(mPager.getCurrentItem()-1);
//                        txt_pageno.setText((position+1) + "/" + pics.size());
                    };
                }
            }
        });
    }
    //代码中设置沉浸式
    private void setImmersive() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            int flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
            int flagTranslucentNavigation = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                Window window = getWindow();
                WindowManager.LayoutParams attributes = window.getAttributes();
                attributes.flags |= flagTranslucentNavigation;
                window.setAttributes(attributes);
                getWindow().setStatusBarColor(Color.TRANSPARENT);
            } else {
                Window window = getWindow();
                WindowManager.LayoutParams attributes = window.getAttributes();
                attributes.flags |= flagTranslucentStatus | flagTranslucentNavigation;
                window.setAttributes(attributes);
            }
        }
    }
    /**
     *
     * @param context
     * @param picList
     * @param pos
     * @param ifLocalPic
     */
    public static void preview(Context context, ArrayList<String> picList, int pos, boolean ifLocalPic) {
        Bundle bundle1 = new Bundle();
        bundle1.putStringArrayList("piclists", picList);
        bundle1.putInt("position", pos);
        bundle1.putBoolean("ifCanDelete", false);
        Intent intent = new Intent(context, ShowPicActivity.class);
        intent.putExtras(bundle1);
        context.startActivity(intent);
    }

    /**
     *
     * @param context
     * @param picList
     * @param pos
     * @param ifLocalPic 是否本地图片
     */
    public static void previewCanDelete(Activity context, ArrayList<String> picList, int pos, boolean ifLocalPic, int requestCode, boolean isCanDelete) {
        Bundle bundle1 = new Bundle();
        bundle1.putStringArrayList("piclists", picList);
        bundle1.putInt("position", pos);
        bundle1.putBoolean("ifCanDelete", isCanDelete);
        bundle1.putBoolean("ISLOCALPIC", ifLocalPic);
        Intent intent = new Intent(context, ShowPicActivity.class);
        intent.putExtras(bundle1);
        context.startActivityForResult(intent, requestCode);
    }

    @Override
    public void onBackPressed() {
        if (isCanDelete && isDoDeleteBtn) {
            Intent intent = new Intent();
            intent.putExtra("delted_list", deletePics);
            setResult(Activity.RESULT_OK, intent);
            finish();
        }else {
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
